package conf2.ui;

import conf2.iface.IGamePieceListener;
import conf2.model.EActor;
import conf2.model.EActor.Side;
import conf2.model.ETerritory;
import conf2.text.Actors;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class PlacementPanel extends JPanel implements ActionListener {

    private final BufferedImage bgImage;
    private ActorInfoPanel actorViewer = null;
    private final ETerritory[] validTerritories;
    private final Color homePolyColor = new Color(0, 0, 255, 168);
    private final Color polyColor = new Color(0, 255, 0, 168);
    private final JButton okButton,  resetButton;
    private List<Listener> listeners = new LinkedList<Listener>();

    public static interface Listener {

        void okClicked();

        void resetClicked();
    }

    public void setOKButtonEnabled(boolean b) {
        okButton.setEnabled(b);
    }

    public PlacementPanel(
            final MainWindow mw,
            final Collection<GamePiece> pieces,
            final ETerritory[] validTerritories,
            final ActorInfoPanel actorViewer,
            final Map<EActor, Point> unplacedPositions) {

        this.validTerritories = validTerritories;

        setLayout(null);

        bgImage = mw.getBackgroundImage();

        for (GamePiece p : pieces) {
            p.addGamePieceListener(new IGamePieceListener() {

                public void pieceDropped(GamePiece source, Point where) {
                }

                public void pieceClicked(GamePiece source, boolean doubleClick, int keyModifiers) {
                    if (doubleClick) {
                        return;
                    }
                    if (keyModifiers != 0) {
                        return;
                    }

                    EActor ea = source.getActor();
                    String name = Actors.getName(ea);
                    String desc = Actors.getDescription(ea);
                    int atkVal = ea.getDefaultAttackValue();
                    Image img = Portraits.get(ea);
                    Side side = ea.getSide();

                    actorViewer.setValues(name, desc, atkVal, img, side);
                    actorViewer.setVisible(true);
                    actorViewer.repaint();
                }

                public void pieceAltClicked(GamePiece source, boolean doubleClick, int keyModifiers) {
                }

                public void pieceDragged(GamePiece source, Point where) {
                }

                public void pieceReleased(GamePiece aThis) {
                }
            });
            add(p);
            p.setVisible(true);
        }


        setSize(bgImage.getWidth(), bgImage.getHeight());
        setPreferredSize(getSize());

//        add(actorViewer);
//        actorViewer.setVisible(true);

        okButton = new JButton("OK");
        okButton.setMnemonic('O');
        okButton.addActionListener(this);
        okButton.setToolTipText("Accepts the current placement.");
        okButton.setEnabled(false);

        resetButton = new JButton("Reset");
        resetButton.setMnemonic('R');
        resetButton.addActionListener(this);
        resetButton.setToolTipText("Removes all pieces from the board.");

        ButtonPanel bp = new ButtonPanel();
        bp.setSize(200, 25);
        bp.setLocation(getWidth() - bp.getWidth() - 3, getHeight() - bp.getHeight() - 3);
        bp.setLayout(new GridLayout(1, 3, 5, 0));
        bp.add(okButton);
        bp.add(resetButton);

        add(bp);


        actorViewer.setLocation(mw.getContentPane().getWidth() - actorViewer.getWidth(), 0);
        actorViewer.setVisible(false);
        add(actorViewer);

        this.actorViewer = actorViewer;
    }

    public void setResetButtonEnabled(boolean b) {
        resetButton.setEnabled(b);
    }

    public void addListener(Listener l) {
        listeners.add(l);
    }

    public void actionPerformed(ActionEvent e) {
        JButton source = (JButton) e.getSource();
        if (source == okButton) {
            for (Listener l : listeners) {
                l.okClicked();
            }
        } else if (source == resetButton) {
            for (Listener l : listeners) {
                l.resetClicked();
            }
        }
    }

    private class ButtonPanel extends JPanel {

        public ButtonPanel() {
            setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle bounds = g.getClipBounds();
            Rectangle r = SwingUtilities.convertRectangle(this, bounds, getParent());
            Image subImg = bgImage.getSubimage(r.x, r.y, r.width, r.height);
            g.drawImage(subImg, bounds.x, bounds.y, null);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        Rectangle r = g.getClipBounds();
        Image subImg = bgImage.getSubimage(r.x, r.y, r.width, r.height);
        g.drawImage(subImg, r.x, r.y, null);

        for (ETerritory et : validTerritories) {
            Polygon poly = RegionBounds.get(et);
            Color c;
            if (et == ETerritory.SHIRE || et == ETerritory.MORDOR) {
                c = homePolyColor;
            } else {
                c = polyColor;
            }

            g.setColor(c);
            g.fillPolygon(poly);

        }
    }
}
